﻿package igz.fleaxy.ui.calendar {
	import adobe.utils.CustomActions;
	import bbv11.widget.currency.ui.WidgetCurrencyUi;
	import com.adobe.net.DynamicURLLoader;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import igz.fleaxy.ui.LinkUi;
	import igz.fleaxy.ui.text.LabelUi;
	import igz.fleaxy.util.DateTimeTypes;
	import igz.fleaxy.util.DateTimeUtil;
	import igz.fleaxy.util.ObjectUtil;
	import mx.controls.Label;
	
	/**
	 * ...
	 * @author Marcelo
	 */
	public class CalendarMultiUi extends Sprite {
		
		protected var _Calendars:Array;
		protected var _Months:Array;
		protected var _Background:Sprite;
		protected var _Calendarios:int;
		protected var _MarginX:int;
		protected var prev:LinkUi;
		protected var next:LinkUi;
		protected var _Fecha:Date;
		protected var _LabelMonths:Array;
	
	protected var _Settings:Object = { "bgColor" : 0xFFFFFF // Color de fondo de lo conjunto de calendarios
										,"bgAlpha" : 1 //Transparencia de color de fondo de conjunto de calendarios
										,"paddingH" : 20 // La margin ezquierda, si esta vazia los calendario si centran
										,"center" : true
										,"width" : null
										,"labelStyle" : "defaultText"
										,"month" : "30/04/2010"
										,"labelMonths" : ""
										,"calendarSettings" : { "paddingLeft" : 1
										,"dayMarginX" : 0
										,"dayMarginY" : 0
										,"border" : 1
										,"defaultDate" : ""
										,"controlMonthVisible" : false
										
										,"daySettings" : { "dayWidth" : 23
										,"dayHeight" : 23
										}
										
										,"dayWeekSettings" : { "dayWeekWidth" : 23
										,"dayWeekHeight" : 19
										}
										
										}
	}
	
	
	
	/**
		 * Componente Calendar
		 *
		 * @param   p_settings = { "bgColor" : 0xFFFFFF // Color de fondo de lo conjunto de calendarios
		 *							,"bgAlpha" : 1 //Transparencia de color de fondo de conjunto de calendarios
		 *							,"paddingH" : 20 // La margin ezquierda, si esta vazia los calendario si centran
		 *							,"center" : true
		 *							,"width" : null
		 *							,"labelStyle" : "defaultText"
		 *							,"month" : "30/04/2010"
		 *							,"labelMonths" : ""
		 *							,"calendarSettings" : { "paddingLeft" : 1
		 *							,"dayMarginX" : 0
		 *							,"dayMarginY" : 0
		 *							,"border" : 1
		 *							,"defaultDate" : ""
		 *							,"controlMonthVisible" : false
		 *							
		 * 							"calendarSettings" : { "bgColor" : 0x000000 // Color de fondo de calendario
		 *								  ,"bgAlpha" : 0 // Transparencia de fondo de calendario
		 *								  ,"bgImage" : null
		 *								  ,"paddingLeft" : 1
		 *								  ,"paddingTop" : 1 
		 *								  ,"dayMarginX" : 5
		 *								  ,"dayMarginY" : 0
		 *								  ,"bgColorDayWeeks" : 0xFFFFFF // Color de fondo de barra de dias de semana
		 *								  ,"bgAlphaDayWeeks" : 1 // Transparencia de fondo de barra de semana
		 *								  ,"bgColorDaysContainer" : 0xFFFFFF // Color de fondo de los dias de calendarios
		 *								  ,"bgAlphaDaysContainer" : 1 // Transparencia de color de fondo de los dias de calendario
		 *								  ,"controlMonthVisible" : true //Si pone a false, cuando estamos usando CalendarMultiUi, pues este ya tienes una barra de navegación
		 *								  ,"monthNavigatorAlign" : "left" //Alignamento del  mes
		 *								  ,"bgColorNavigator" : 0xFFFFFF //Color de fondo de barra de navegación
		 *								  ,"bgAlphaNavigator" : 0 //Transparencia de fondo de barra de navegación
		 *								  ,"iconPrev" : null // Icon para volver en mes
		 *								  ,"iconNext" : null // Icon para avanzar en mes
		 *								  ,"selectedMonth" : null//(Date) date con el mes que queremos mostrar en el calendario
		 *								  ,"defaultDate" : null //(Date) Fecha (si aplica) Cuando esteja a uno el calendario si auto fijara en la labelDate
		 *								  ,"specialDates" : null // Array de SpecialDate
		 * 
		 * 								  ,"daySettings" : {	"dayWidth" : 23 //Ancho de la caja del dia
		 *								  ,"dayHeight" : 23 //Altura de la caja del dia
		 *								  ,"dayBg" : 0xFFFFFF //Color de fondo de la caja
		 *								  ,"bgAlpha" : 1 //Tranparencia de la caja de fondo 
		 *								  ,"dayBgWeekEnd" : 0xFAAC58 //Color de los dias en fin de semana
		 *								  ,"dayBgColorOver" : 0xe34c26 //Color de fondo de la caja
		 *								  ,"dayAlphaOver" : 0 //Color //Tranparencia de la caja
		 *								  ,"dayBgColorBorderOver" : 0xD3DBDE //Color del border
		 *								  ,"dayBgBorderOver" : 0 //Ancho del liena de border
		 *								  ,"dayLabelStyle" : "defaultTextDay" //Style de un dia normal
		 *								  ,"dayLabelStyleOver" : "defaultTextDay" //Style de un dia normal
		 *								  ,"dayLabelStyleToday" : "defaultTextToday" //Style de lo dia de hoy
		 *								  ,"dayLabelStyleTodayOver" : "defaultTextToday" //Style de lo dia de hoy
		 *								  ,"dayLabelStyleSpecial" : "defaultTextSpecial" //Style de los special day
		 *								  ,"dayLabelStyleSpecialOver" : "defaultTextSpecial" //Style de los special day
		 *								  ,"specialDate" : null // SpecialDate
		 *								  ,"onSelectDay" : null // function ( p_date:Date ) {}
		 *								  ,"unSelectDay" : null // function ( p_date:Date ) {}
		 *								  , "onSelectSpecialDay" : null // function ( p_specialDate:SpecialDate ) {}
		 * 									}
		 * 
		 * 								,"dayWeekSettings" : { "dayWeekWidth" : 23 //Ancho de la caja de dias de semana
		 *								 ,"dayWeekHeight" : 19 //Altura de la caja de dias de semana
		 *								 ,"dayWeekBgColor" : 0xFFFFFF //Color de fondo de los dias de semana
		 *								 ,"dayWeekBgAlpha" : 1 //Transparencia de fondo de dias de semana
		 *								 ,"dayWeekLabelStyle" : "defaultText" //Style
		 *								 ,"labelWeekDays" : "" // "D,L,M,M,J,V,S" Si no le pasamos elle corre por defecto de sistema
		 *								}
		 * 
		 *							}
		 * 						};
		 *
		 * @example
		 *
		 * 
		 * var _Calendar:CalendarUi = new CalendarUi({"bgColor" : 0x000000 ,"bgAlpha" : 0, { "daySettings" : "dayBgColorBorderOver" : 0xD3DBDE, "dayBgBorderOver" : 1 , "onSelectSpecialDay" : _onSelectSpecialDate}});
		 *
		 * addChild( _Calendar );
		 *
		 */
	
	
	
	
	/**
		 * Clase permite crear n calendarios.
		 *
		 * @param	p_calendarios : (Numero de calendarios)
		 * @param	p_settings   : { "bgColor" : 0x000000 // Color de fondo de calendario
										  ,"bgImage" : null
										  ,"bgAlpha" : 0.2 // Transparencia de fondo de calendario
										  ,"paddingLeft" : 1
										  ,"paddingTop" : 1
										  ,"dayMarginX" : 5
										  ,"dayMarginY" : 1
										  ,"bgColorDayWeeks" : 0xFFFFFF // Color de fondo de barra de dias de semana
										  ,"bgAlphaDayWeeks" : 0.2 // Transparencia de fonfo de barra de semana
										  ,"bgColorDaysContainer" : 0xFFFFFF // Color de fondo de los dias de calendarios
										  ,"bgAlphaDaysContainer" : 0.2 // Transparencia de color de fondo de los dias de calendario
										  ,"navigatorAlign" : "left"
										  ,"border" : 1
										  ,"labelDate" : "" // Fecha por defecto en calendario
										  ,"dafaultFecha" : 0
										  ,"iconPrev" : null // Icon para volver en mes
										  ,"iconNext" : null // Icon para avanzar en mes
										  ,"controlMonthVisible" : true
										  , "specialDates" : null // Array de SpecialDate
										  
										  ,"daySettings" : { "dayWidth" : 23
										  ,"dayHeight" : 23
										  ,"dayBg" : 0xFFFFFF
										  ,"dayBgWeekEnd" : 0xFAAC58
										  ,"dayBgColorOver" : 0xe34c26
										  ,"dayBgColorSelected" : 0xe34c26
										  ,"dayLabelStyle" : "defaultText"
										  ,"dayLabelStyleOver" : "defaultText"
										  ,"dayLabelStyleToday" : "defaultTextToday"
										  ,"dayLabelStyleTodayOver" : "defaultTextToday"
										  ,"dayLabelStyleSpecial" : "defaultTextSpecial"
										  ,"dayLabelStyleSpecialOver" : "defaultTextSpecial"
										  ,"specialDate" : null // SpecialDate // Array de fechas especiales
										  ,"onSelectDay" : null // function ( p_date:Date ) {}
										  ,"unSelectDay" : null // function ( p_date:Date ) {}
										  ,"onSelectSpecialDay" : null // function ( p_specialDate:SpecialDate ) {}
													  }
										  ,"dayWeekSettings" : { "dayWeekWidth" : 23
															    ,"dayWeekHeight" : 19
															    ,"dayWeekBgColor" : 0xff0000
															    ,"dayWeekBgAlpha" : 0
															    ,"dayWeekLabelStyle" : "defaultText"
																,"labelWeekDay" : null ex: "D,L,M,M,J,V,S"
													  }
										}
		 *
		 * @example
		 *
		 * var _SpecialDates:Array = new Array();
		 * _SpecialDates.push(new SpecialDate(new Date(2010, 02, 10), 0x00FF00));
		 * var calendarMulti:CalendarMultiUi = 
		 * 								new CalendarMultiUi(p_calendarios,
		 * 									{ "iconPrev" : ig27_formacion02.$PV_BG, "iconNext" : ig27_formacion02.$NT_BG, 
		 * 									"calendarSettings" : {"labelDate" : _Fecha.$Value, "specialDates" : _SpecialDates, 
		 * 									"bgColor" : "0x000000", 
		 * 									"daySettings" : {"dayLabelStyleSpecial" : "defaultTextSpecial", 
		 * 									"dayLabelStyleSpecialOver" : "defaultTextSpecialOver", 
		 * 									"onSelectDay" : _SelectDate, "onSelectSpecialDay" : _SelectSpecialDate, 
		 * 									"unSelectDay" : _UnselectDay}}});
		 * 									
		 * var link:LinkUi = new LinkUi(label,
		 * 								{ onClick : metodo1
		 * 								 , onRollOver : metodo2
		 * 								 , onRollOut : metodo3 }
		 * 								);
		 * addChild(link);
		 *
		 */

	
		public function CalendarMultiUi(p_calendarios:int = 2, p_settings:Object = null) {
			ObjectUtil.$Merge(p_settings, _Settings);
			_Calendarios = p_calendarios;
			
			_Calendars = new Array();
			_Months = new Array();
			_Background = new Sprite();
			
			_MarginX = _GetMarginX();
			
			
			// Carga los meses
			if ((_Settings["labelMonths"] as String).split(",").length == 12) {
					_LabelMonths = (_Settings["labelMonths"] as String).split(",");
			} else {
					_LabelMonths = new Array();
					for ( var f:int = 0; f < 12; f++) {
						var date:Date = new Date(2010, 0, 1);
						date.setMonth(date.month + f);
						_LabelMonths.push(DateTimeUtil.$GetValue(date, DateTimeTypes.MONTH_NAME));
					}
			}
			
			if (DateTimeUtil.$Validate(_Settings["month"])) {
				_Fecha = DateTimeUtil.$ToDate(_Settings["month"]);
			}
			else {
				_Fecha = new Date();
			}
			
			// Inicializar CalendarUi
			
			var fecha:Date = DateTimeUtil.$Clone(_Fecha);
			for ( var i:int = 0; i < _Calendarios; i++) {
				var calendar:CalendarUi;
				fecha.setDate(1);
				fecha.month += i;
				_Months[i] = new LabelUi(String(_LabelMonths[fecha.month]).toLowerCase() + " " + DateTimeUtil.$GetValue(fecha, DateTimeTypes.YEAR), _Settings["labelStyle"]);
				
				_Background.addChild(_Months[i]);
				_Settings["calendarSettings"]["selectedMonth"] = fecha;
				calendar = new CalendarUi(_Settings["calendarSettings"]);
				
				_Calendars.push(calendar);
				_Background.addChild(calendar);
				
				calendar.addEventListener(MouseEvent.CLICK, _UnselectedDay);
			}
			
			//Botones
						if (_Settings["iconPrev"] is Bitmap) {
							prev = new LinkUi(_Settings["iconPrev"], { "onClick" : _GoPrevMonth } );
						} else {
							prev = new LinkUi(new LabelUi("&lt;", _Settings["labelStyle"]), { "onClick" : _GoPrevMonth } );
						}
						_Background.addChild(prev);
					
					
						if (_Settings["iconNext"] is Bitmap) {
							next = new LinkUi(_Settings["iconNext"], { "onClick" : _GoNextMonth } );
						} else {
							next = new LinkUi(new LabelUi("&gt;", _Settings["labelStyle"]), { "onClick" : _GoNextMonth } );
						}
						_Background.addChild(next);
		
			$Draw();
			addChild(_Background);
		}
		
		public function $Draw():void {
			for ( var i:int = 0; i < _Calendarios; i++) {
				(_Months[i] as LabelUi).x = _Settings["paddingH"] + (_GetCalendarWidth() * i) + (_MarginX * i) + ((_GetCalendarWidth() - (_Months[i] as LabelUi).width) >> 1 );
				(_Months[i] as LabelUi).y = 0;
				
				(_Calendars[i] as CalendarUi).x = _Settings["paddingH"] + (_GetCalendarWidth() * i) + (_MarginX * i);
				(_Calendars[i] as CalendarUi).y = (_Months[i] as LabelUi).height;
				
				//Botones
					if (i == 0)  {
						prev.x = _Settings["paddingH"];
						prev.y = (_Months[i] as LabelUi).y + 4;
					}
					
					
					if (i == (_Calendarios -1)) {
						next.x = _Settings["paddingH"] + (_GetCalendarWidth() * i) + (_MarginX * i) + (_GetCalendarWidth() - next.width);
						next.y = (_Months[i] as LabelUi).y + 4;
					}
			}
			
			_Background.graphics.clear();
			_Background.graphics.beginFill( _Settings["bgColor"], _Settings["bgAlpha"] );
			_Background.graphics.drawRoundRect( 0, 0, _Settings["width"], _GetCalendarHeigth() + 60, 10);
			_Background.graphics.endFill();
			//var fecha:Date = DateTimeUtil.$Clone(_Fecha);
			//for ( var i:int = 0; i < _Calendarios; i++) {
				//var calendar:CalendarUi;
				//fecha.setDate(1);
				//fecha.month += i;
				//_Months[i] = new LabelUi(_LabelMonths[fecha.month] + " " + DateTimeUtil.$GetValue(fecha, DateTimeTypes.YEAR), _Settings["labelStyle"]);
				//(_Months[i] as LabelUi).x = _Settings["paddingH"] + (_GetCalendarWidth() * i) + (_MarginX * i) + ((_GetCalendarWidth() - (_Months[i] as LabelUi).width) >> 1 );
				//(_Months[i] as LabelUi).y = 0;
				//
				//_Background.addChild(_Months[i]);
				//_Settings["calendarSettings"]["month"] = fecha;
				//calendar = new CalendarUi(_Settings["calendarSettings"]);
				//calendar.x = _Settings["paddingH"] + (_GetCalendarWidth() * i) + (_MarginX * i);
				//calendar.y = (_Months[i] as LabelUi).height;
				//_Calendars.push(calendar);
				//_Background.addChild(calendar);
				//
				//calendar.addEventListener(MouseEvent.CLICK, _UnselectedDay);
				//
				//Botones
					//if (i == 0 && prev == null)  {
						//if (_Settings["iconPrev"] is Bitmap) {
							//prev = new LinkUi(_Settings["iconPrev"], { "onClick" : _GoPrevMonth } );
						//} else {
							//prev = new LinkUi(new LabelUi("&lt;", _Settings["labelStyle"]), { "onClick" : _GoPrevMonth } );
						//}
						//prev.x = _Settings["paddingH"];
						//prev.y = 0;
						//_Background.addChild(prev);
					//}
					//
					//
					//if (i == (_Calendarios -1) && next == null) {
						//if (_Settings["iconNext"] is Bitmap) {
							//next = new LinkUi(_Settings["iconNext"], { "onClick" : _GoNextMonth } );
						//} else {
							//next = new LinkUi(new LabelUi("&gt;", _Settings["labelStyle"]), { "onClick" : _GoNextMonth } );
						//}
						//next.x = _Settings["paddingH"] + (_GetCalendarWidth() * i) + (_MarginX * i) + (_GetCalendarWidth() - next.width);
						//next.y = 0;
						//_Background.addChild(next);
					//}
			//}
		}
		
		public function $getWidth():int {
			return _Settings["width"];
		}
		
		public function $setBackgroud(bgColor:uint, bgAlpha:Number = -1):void {
			if (bgAlpha != -1 ) {
				_Settings["bgAlpha"] = bgAlpha;
			}
			_Background.graphics.clear();
			_Background.graphics.beginFill( bgColor,_Settings["bgAlpha"] );
			_Background.graphics.drawRoundRect( 0, 0, _Settings["width"], _GetCalendarHeigth() + 60, 10);
			_Background.graphics.endFill();
		}
		
		public function $setStyleHeader(p_style:String):void {
			for ( var i:int = 0; i < _Calendarios; i++) {
				(_Months[i] as LabelUi).$StyleName = p_style;
			}
		}
		
		public function $setStyleDay(p_style:String):void {
			for ( var i:int = 0; i < _Calendarios; i++) {
				(_Calendars[i] as CalendarUi).$setStyleDay(p_style);
			}
		}
		
		public function $setStyleWeekDay(p_style:String):void {
			for ( var i:int = 0; i < _Calendarios; i++) {
				(_Calendars[i] as CalendarUi).$setStyleWeekDay(p_style);
			}
		}
		
		public function $setSpecialDays(p_specialDates:Array):void {
			for (var i:int = 0; i < _Calendarios; i++) {
				CalendarUi(_Calendars[i]).$SetSpecialDates(p_specialDates);
			}
		}
		
		protected function _GoPrevMonth(p_event:MouseEvent):void
		{
			for ( var i:int = 0; i < _Calendarios; i++) {
				(_Calendars[i] as CalendarUi).$GoPreviousMonth();
				(_Months[i] as LabelUi).text = String(_LabelMonths[(_Calendars[i] as CalendarUi).$Date.month]).toLowerCase() + " " + DateTimeUtil.$GetValue((_Calendars[i] as CalendarUi).$Date, DateTimeTypes.YEAR);
				(_Months[i] as LabelUi).x = _Settings["paddingH"] + (_GetCalendarWidth() * i) + (_MarginX * i) + ((_GetCalendarWidth() - (_Months[i] as LabelUi).width) >> 1 );
			}
		}
		
		protected function _GoNextMonth(p_event:MouseEvent):void
		{
			for ( var i:int = 0; i < _Calendarios; i++) {
				(_Calendars[i] as CalendarUi).$GoNextMonth();
				(_Months[i] as LabelUi).text = String(_LabelMonths[(_Calendars[i] as CalendarUi).$Date.month]).toLowerCase() + " " + DateTimeUtil.$GetValue((_Calendars[i] as CalendarUi).$Date, DateTimeTypes.YEAR);	
				(_Months[i] as LabelUi).x = _Settings["paddingH"] + (_GetCalendarWidth() * i) + (_MarginX * i) + ((_GetCalendarWidth() - (_Months[i] as LabelUi).width) >> 1 );
			}
		}
		
		protected function _UnselectedDay(p_event:MouseEvent):void {
				for (var i:int = 0; i < _Calendarios; i++) {
					if ((p_event.currentTarget as CalendarUi) != _Calendars[i] || (p_event.currentTarget as CalendarUi).$Fecha == null) {
						(_Calendars[i] as CalendarUi).$Unselect();
						(_Calendars[i] as CalendarUi).$Fecha = (p_event.currentTarget as CalendarUi).$Fecha;
					}
				}
		}
		
		protected function _GetCalendarWidth():int {
			return _Settings["calendarSettings"]["paddingLeft"] + (_Settings["calendarSettings"]["dayMarginX"] * 6) + (_Settings["calendarSettings"]["daySettings"]["dayWidth"] * 7) + (_Settings["calendarSettings"]["border"] * 2);
		}
		
		protected function _GetCalendarHeigth():int  {
			return	(_Settings["calendarSettings"]["dayMarginY"] * 4) + (_Settings["calendarSettings"]["daySettings"]["dayHeight"] * 5) + (_Settings["calendarSettings"]["dayWeekSettings"]["dayWeekHeight"]) + (_Settings["calendarSettings"]["border"] * 2);
		}
		
		protected function _GetMarginX():int {
			var marginx:int = 20;
			if (_Settings["width"] != null) {
				if (_Settings["center"] == false) {
					marginx = (_Settings["width"] - ((_Settings["paddingH"] * 2) + (_GetCalendarWidth() * _Calendarios))) / (_Calendarios - 1);
				} else {
					_Settings["paddingH"] = (_Settings["width"] - (_Calendarios * _GetCalendarWidth() + marginx * (_Calendarios - 1))) / 2;
					marginx = (_Settings["width"] - ((_Settings["paddingH"] * 2) + (_GetCalendarWidth() * _Calendarios))) / (_Calendarios - 1);
				}
			} else {
				_Settings["width"] = (_Settings["paddingH"] * 2) + (_GetCalendarWidth() * _Calendarios) + (_MarginX * (_Calendarios - 1));
			}
			return marginx;
		}
		
		public function $SetWidthNavigator(p_width:int) : void {
			_Settings["width"] = p_width;
			_MarginX = _GetMarginX();
			$Draw();
		}
		
		public function $SetStyle(p_style:String):void {
			for ( var i:int = 0; i < _Months.length; i++) {
				LabelUi(_Months[i]).$StyleName = p_style;
			}
		}
		
	}

}